package com.zmn.plat.admin.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.mcc.cas.client.CasClient;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.common.dto.staff.StaffDRO;
import com.zmn.mcc.dubbo.dto.DubboConsts;
import com.zmn.mcc.dubbo.interfaces.staff.StaffListRemoteService;
import com.zmn.plat.business.interfaces.feedback.FeedbackBService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @author tanbiao
 * @description
 * @modifier
 * @since 2020/6/25 17:51
 **/
@Controller
@RequestMapping("/")
public class IndexListController extends PlatBaseController {
    private Logger logger = LoggerFactory.getLogger(IndexListController.class);
    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private StaffListRemoteService staffListRemoteService;

    @Autowired
    private FeedbackBService feedbackBService;

    @Autowired
    private CasClient casClient;

    /**
     * 主页面
     */
    @RequestMapping(value = {"index", "/"})
    public ModelAndView index(HttpServletRequest request) {
        LoginStaff staff = getMcStaff();
        ModelAndView mav = new ModelAndView("index");
        mav.addObject("staffName", staff.getRealName());
        mav.addObject("staffId", staff.getStaffId());
        mav.addObject("logoutUrl", casClient.getLogoutUrl());
        mav.addObject("updatePwd", casClient.getUpdatePwd());

        // 从数据库查询最新的实体，解决修改密码框弹出时获取shiro中initialPassword没有发生变化的问题
        ResponseDTO<List<StaffDRO>> responseDTO =
                staffListRemoteService.listStaffByStaffIds(Arrays.asList(staff.getStaffId()), GlobalConsts.YES);
        if (responseDTO.isSuccess() && !CollectionUtils.isEmpty(responseDTO.getData())) {
            StaffDRO dro = responseDTO.getData().get(0);
            mav.addObject("initialPassword", dro.getInitPwd());
        }

        return mav;
    }

    /**
     * console页面
     */
    @RequestMapping(value = {"console"})
    public ModelAndView console(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("console");
        return mav;
    }

    /**
     * 反馈按钮
     */
    @RequestMapping(value = {"feedback"})
    @ResponseBody
    public String feedback(HttpServletRequest request) {
        LoginStaff staff = getMcStaff();
        // 从数据库查询最新的实体，解决修改密码框弹出时获取shiro中initialPassword没有发生变化的问题
        ResponseDTO<List<StaffDRO>> responseDTO =
                staffListRemoteService.listStaffByStaffIds(Arrays.asList(staff.getStaffId()), GlobalConsts.YES);
        if (responseDTO.isSuccess() && !CollectionUtils.isEmpty(responseDTO.getData())) {
            StaffDRO dro = responseDTO.getData().get(0);
            //反馈按钮需要的字段
            return feedbackBService.getEncrypt(dro.getDTalkId());
        }
        return StringUtils.EMPTY;
    }
}
